﻿<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:De.LarsHildebrandt.WpfControls.WpfSimpleChart"
    xmlns:converters="clr-namespace:De.LarsHildebrandt.WpfControls.WpfSimpleChart.Converters">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="/De.LarsHildebrandt.WpfControls.WpfSimpleChart;component/ResourceDictionaries/ChartToolTip.xaml" />
    </ResourceDictionary.MergedDictionaries>
    
    <converters:BarChartItemWidthConverter x:Key="itemWidthConverter"/>
    <converters:BarChartItemHeightConverter x:Key="itemHeightConverter"/>
    <converters:BarChartItemVisibilityRowDefinitionConverter x:Key="itemVisibilityRowDefinitionConverter"/>
    <converters:YAxisMarginConverter x:Key="yAxisMarginConverter"/>

    <Style TargetType="{x:Type local:BarChart}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:BarChart}">
                    <Border
                        Background="{Binding Path=ChartBackground, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:BarChart}, AncestorLevel=1}}"
                        BorderBrush="{Binding Path=ChartBackgroundBorderColor, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:BarChart}, AncestorLevel=1}}"
                        BorderThickness="{Binding Path=ChartBackgroundBorderThickness, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:BarChart}, AncestorLevel=1}}"
                        CornerRadius="{Binding Path=ChartBackgroundCornerRadius, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:BarChart}, AncestorLevel=1}}"
                        >
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="*" />
                                <RowDefinition Height="Auto" />
                            </Grid.RowDefinitions>
                            <Label Grid.Row="0" HorizontalAlignment="Center" FontSize="12">
                                <Label.Content>
                                    <Binding Path="Caption" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type local:BarChart}, AncestorLevel=1}"/>
                                </Label.Content>
                            </Label>
                            <Grid Grid.Row="1"> 
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="*" />
                                </Grid.ColumnDefinitions>
                                <Grid Grid.Column="0" Width="40" Background="Beige" x:Name="yAxisLine" Visibility="Collapsed">
                                    <Grid.Margin>
                                        <MultiBinding Converter="{StaticResource yAxisMarginConverter}" UpdateSourceTrigger="PropertyChanged">
                                            <Binding Path="5,5,5,5"/>
                                            <Binding Path="ChartMargin" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type local:BarChart}, AncestorLevel=1}"/>
                                        </MultiBinding>
                                    </Grid.Margin>
                                    <Line 
                                        X1="10"
                                        X2="10"
                                        Y1="{Binding Path=ActualHeight, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Grid}, AncestorLevel=1}}"
                                        Y2="{Binding Path=ActualWidth, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Grid}, AncestorLevel=1}}"
                                        Stroke="Red"
                                        StrokeThickness="1"
                                        >
                                    </Line>
                                </Grid>
                                <ScrollViewer Grid.Column="1" x:Name="scrollViewer"
                                    HorizontalScrollBarVisibility="{Binding Path=ChartScrollBarVisibility, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:BarChart}, AncestorLevel=1}}"
                                    VerticalScrollBarVisibility="{Binding Path=ChartScrollBarVisibility, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:BarChart}, AncestorLevel=1}}">
                                    <ItemsControl 
                                        x:Name="itemsControl" 
                                        ItemsSource="{Binding}"
                                        Margin="5"
                                        Grid.Row="1">
                                        <ItemsControl.ItemsPanel>
                                            <ItemsPanelTemplate>
                                                <StackPanel IsItemsHost="True" Orientation="Horizontal"/>
                                            </ItemsPanelTemplate>
                                        </ItemsControl.ItemsPanel>
                                        <ItemsControl.ItemTemplate>
                                            <DataTemplate>
                                                <Grid>
                                                    <Grid.RowDefinitions>
                                                        <RowDefinition Height="*" />
                                                        <RowDefinition Height="Auto" />                                                            
                                                    </Grid.RowDefinitions>
                                                    <Border
                                                        x:Name="border"
                                                        VerticalAlignment="Bottom"
                                                        Grid.Row="0"
                                                        CornerRadius="{Binding Path=ChartCornerRadius, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:BarChart}, AncestorLevel=1}}"
                                                        Margin="{Binding Path=ChartMargin, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:BarChart}, AncestorLevel=1}}"
                                                        BorderThickness="{Binding Path=ChartBorderThickness, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:BarChart}, AncestorLevel=1}}"
                                                        BorderBrush="{Binding Path=ChartBorderColor, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:BarChart}, AncestorLevel=1}}"
                                                        Background="{Binding Path=ChartBrush, UpdateSourceTrigger=PropertyChanged}"
                                                        >
                                                        <Border.ToolTip>
                                                            <ToolTip Style="{StaticResource toolTipStyle}" />
                                                        </Border.ToolTip>
                                                        <Border.Height>
                                                            <MultiBinding Converter="{StaticResource itemHeightConverter}" UpdateSourceTrigger="PropertyChanged">
                                                                <Binding Path="Value"/>
                                                                <Binding Path="ActualHeight" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type ItemsControl}, AncestorLevel=1}"/>
                                                                <Binding Path="Items" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type ItemsControl}, AncestorLevel=1}"/>
                                                            </MultiBinding>
                                                        </Border.Height>
                                                        <Border.Width>
                                                            <MultiBinding Converter="{StaticResource itemWidthConverter}" UpdateSourceTrigger="PropertyChanged">
                                                                <Binding Path="ActualWidth" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type ItemsControl}, AncestorLevel=1}"/>
                                                                <Binding Path="Items.Count" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type ItemsControl}, AncestorLevel=1}"/>
                                                                <Binding Path="ChartMargin" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type local:BarChart}, AncestorLevel=1}"/>
                                                                <Binding Path="ShowScrollbars" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type local:BarChart}, AncestorLevel=1}"/>
                                                                <Binding Path="ChartItemWidth" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type local:BarChart}, AncestorLevel=1}"/>
                                                            </MultiBinding>
                                                        </Border.Width>
                                                        <Label 
                                                            Visibility="{Binding Path=ChartValuesVisibility, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:BarChart}, AncestorLevel=1}}"
                                                            Content="{Binding Path=Value}"
                                                            HorizontalAlignment="Center">
                                                        </Label>
                                                    </Border>
                                                    <Label 
                                                        Visibility="{Binding Path=ChartCaptionVisibility, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:BarChart}, AncestorLevel=1}}"
                                                        HorizontalAlignment="Center"
                                                        Grid.Row="1" 
                                                        Content="{Binding Path=Caption}">
                                                    </Label>
                                                </Grid>
                                                <DataTemplate.Triggers>
                                                    <Trigger SourceName="border" Property="IsMouseOver" Value="True">
                                                        <Setter 
                                                        TargetName="border" 
                                                        Property="Background" 
                                                        Value="{Binding Path=ChartColorMouseOver, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:BarChart}, AncestorLevel=1}}">
                                                        </Setter>
                                                    </Trigger>
                                                </DataTemplate.Triggers>
                                            </DataTemplate>
                                        </ItemsControl.ItemTemplate>
                                    </ItemsControl>
                                </ScrollViewer>
                            </Grid>
                            
                            
                            <ItemsControl
                                Grid.Row="2"
                                Margin="5"
                                Visibility="{Binding Path=ChartLegendVisibility, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:BarChart}, AncestorLevel=1}}"
                                ItemsSource="{Binding Path=ChartLegend, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:BarChart}, AncestorLevel=1}}">
                                <ItemsControl.ItemsPanel>
                                    <ItemsPanelTemplate>
                                        <WrapPanel IsItemsHost="True">

                                        </WrapPanel>
                                    </ItemsPanelTemplate>
                                </ItemsControl.ItemsPanel>
                                <ItemsControl.ItemTemplate>
                                    <DataTemplate>
                                        <StackPanel Orientation="Horizontal">
                                            <Rectangle Stroke="Black" StrokeThickness="0.5" Height="10" Width="20" Fill="{Binding Path=ItemBrush}"/>
                                            <Label Content="{Binding Path=Caption}"></Label>
                                        </StackPanel>
                                    </DataTemplate>
                                </ItemsControl.ItemTemplate>
                            </ItemsControl>
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>